﻿using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace PlaxoSyncWM {
    public class ParseWebResponse {
        private HttpWebRequest req = null;
        private HttpWebResponse resp = null;
        private StreamReader reader;

        public string guid;
        public string guidString;
        public string[] elements;

        public string getGuid(string guidURL) {
            req = (HttpWebRequest) HttpWebRequest.Create(guidURL);
            resp = (HttpWebResponse) req.GetResponse();
            Regex splitOnRegX = new Regex(@",", RegexOptions.Compiled);
            if (resp.StatusCode == HttpStatusCode.OK & resp != null) {
                string line = null;
                reader = new StreamReader(resp.GetResponseStream());
                while ((line = reader.ReadLine()) != null) {
                    if (line.Contains("GUID")) {
                        elements = splitOnRegX.Split(line);
                        guid = cleanupElement(elements[7]);
                        return guid;
                    }
                }
                req = null;
                resp.Close();
            }
            guid = "none";
            return guid;
        }

        public string cleanupElement(string str) {
            string n1, n2;
            char[] removeThese = {']'};
            n1 = str.Trim();
            n2 = n1.TrimEnd(removeThese);
            return n2;
        }
    }
}